import Vue from "vue";

import Vuex from "vuex"
import createPersistedState from "vuex-persistedstate";

Vue.use(Vuex); // this.$store

// 创建仓库store
const store = new Vuex.Store({
    state: {
        cartData: [],
        token: '',
        userInfo: ''
    },
    // mutations是修改数据的唯一途径
    mutations: {
        setToken(state, token) {
            state.token = token
        },
        setUserInfo(state, userInfo) {
            state.userInfo = userInfo
        },
        updateAvatar(state, newAvatar) {
            state.userInfo.avatar = newAvatar
        },
        // 清空购物车数据
        clearCartData(state) {
            state.cartData = state.cartData.filter(item => item.selected !== true);
        },
        // 往购物车添加商品
        addGoodsToCar(state, goods) {
            // 先判断是否有相同商品，有则累加数量，无直接追加到头部
            let index = state.cartData.findIndex(item => item.id === goods.id)
            console.log(index);
            if (index === -1) {
                state.cartData.unshift(goods)
            } else {
                // 累加数量即可
                state.cartData[index].number += goods.number
            }

        },

        //删除购物车商品
        DeleteSelectedItems(state, id) {
            state.cartData = state.cartData.filter(item => item.id !== id)
        },
        //设置购物车商品选中标签
        ItemSelectedState(state, { id, selected }) {
            let index = state.cartData.findIndex(item => item.id === id)
            state.cartData[index].selected = selected
        },
        // 设置购物车商品购买数量
        setGoodsBuyNum(state, { id, number }) {
            let index = state.cartData.findIndex(item => item.id === id)
            state.cartData[index].number = number
        },
        clearUserInfo(state) {
            state.token = ''
            state.cartData = []
            state.userInfo = {}
        }
    },
    actions: {},
    getters: {
        //获取到购物车的数量
        GetTotalShoppingCarts(state) {
            let GoodsNumber = 0;
            state.cartData.forEach(item => GoodsNumber += item.number)
            return GoodsNumber
        },
        //得到分割的商品id 
        getGoodsIds(state) {
            return state.cartData.map(item => item.id).join(',')
        },
        // 
        getGoodsNumberById(state) {
            let idNumMap = {};
            state.cartData.forEach(item => {
                idNumMap[item.id] = item.number
            })
            return idNumMap;

        },
        getGoodsStatusById(state) {
            let idSelectedMap = {};
            state.cartData.forEach(item => {
                idSelectedMap[item.id] = item.selected
            })
            return idSelectedMap;
        },
        // 获取选中商品数量
        ItemSelectedNum(state) {
            let totalNumber = 0;
            state.cartData.forEach(item => item.selected && (totalNumber += item.number))
            return totalNumber;
        },
        // 获取选中商品数量的总价格
        allPrice(state) {
            let totalPrice = 0;
            state.cartData.forEach(({ sell_price, number, selected }) => selected && (totalPrice += number * sell_price))
            return totalPrice
        }
    },
    plugins: [createPersistedState()]
})

// 暴露出去
export default store
